package com.rockcent.mall.controller;

import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.mall.Routes;
import com.rockcent.mall.lib.domain.Location;
import com.rockcent.mall.lib.dto.LocationDto;
import com.rockcent.mall.lib.dto.UserInfoDto;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import com.rockcent.mall.lib.repository.LocationRepository;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zhangjh on 2016/10/8.
 */
@RestController
public class LocationController extends BaseController {

    private final Logger log = LoggerFactory.getLogger(LocationController.class);

    @Autowired
    private LocationRepository locationRepository;

    @Autowired
    private BaseUserHelper userHelper;


    /**
     *
     * @api {get} /location/info 获取商家所在地区列表
     * @apiGroup location
     * @apiVersion 1.0.0
     * @apiParam {String} parentId <code>可为空，为空表示获取顶级地区信息</code>父级地区ID
     * @apiHeader {String} userToken 用户token
     * @apiHeader {String} domain    请求域名
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "code": "1",
     *  "desc": "成功",
     *  "data": [
     *      {
     *        "id": 210100,
     *        "dateCreated": "2016-10-08 17:46:58",
     *        "lastUpdated": "2016-10-08 17:47:01",
     *        "version": 0,
     *        "fristPinyin": "S",
     *        "level": 2,
     *        "locationName": "沈阳市",
     *        "parentId": "210000",
     *        "state": 0
     *      },
     *      ...
     *   ]
     * }
     */
    @RequestMapping(value = Routes.LOCATION_INFO, method = RequestMethod.GET)
    public JsonResult listMerchantLocations(LocationDto dto, HttpServletRequest request) {
        UserInfoDto userInfoDto=userHelper.getSession(request);
        RestDoing doing = jsonResult -> {

            if (StringUtils.isBlank(dto.getParentId())) {
                dto.setParentId("0");
            }
            List<Location> locationList = locationRepository.getByParentId(dto.getParentId());
            jsonResult.data = locationList;
        };
        return doing.go(request, log);
    }
}
